#include "ProcessHistory.h"


ProcessHistory::ProcessHistory(void)
{
}

void ProcessHistory::fillDates(vector<Date>& historyList, int index)
{
	Date temp=historyList[historyList.size()-1];
	Date temp1;
	for (int i = historyList.size(); i <=index; i++)
	{
		temp.decrementDays(1);
		temp1.setDay(temp.getDay());
		temp1.setMonth(temp.getMonth());
		temp1.setYear(temp.getYear());
		historyList.push_back(temp1);
	}
}

void ProcessHistory::addEvent (vector<Date>& historyList,struct Task currentTask)
{
	Date currentDate;
	currentDate=historyList[0];
	Time time[2];
	Time tempTime;
	Date dateToAdd=currentDate.dateStringToObject(currentTask.date);
	int index = historyList[0].dateDifference(dateToAdd);
	if (historyList.size()<=index)
		fillDates(historyList, index);
	tempTime.getStartEndTime(currentTask.time,time);

	//assert(currentTask.id!="");
	if (currentTask.deadline=="se")
		currentTask.status="d";

	historyList[index].addEvent(currentTask.id,currentTask.name,currentTask.time, currentTask.location, time[0], time[1], currentTask.deadline, currentTask.status);	
}

void ProcessHistory::deleteEvent (vector<Date>& historyList, vector<Event>& pendingList ,struct Task currentTask)
{
	int displayIndex, i;
	Event eventObj;
	
	displayIndex=atoi(currentTask.index.c_str());
	string id = pendingList[displayIndex-1].getId();
	string date=id.substr(0,8);
	Date dateObj=historyList[0].dateStringToObject(date);
	int dateIndex=historyList[0].dateDifference(dateObj);
	int indexToDelete=historyList[dateIndex].getIndex(id);
				
	eventObj=historyList[dateIndex].getEvent(indexToDelete);
	historyList[dateIndex].deleteEvent(indexToDelete);
	//statusMessage = currentList[dateIndex].getEventDetails(indexToDelete);
	//commandOrder.push("delete");
	//eventOrder.push(eventObj);
}

bool ProcessHistory::editEvent (vector<Date>& historyList, vector<Event>& pendingList , vector<Date>& currentList, struct Task currentTask)
{
	Event eventObj;
	ProcessList listObj;
	struct Task newTask;
	bool flag = true;
	int displayIndex, i;
	Time time[2];
	stringstream convertedTime;
	int indexToEdit,dateIndex;
	string id;
	string date;
	Date dateObj;
	

	displayIndex=atoi(currentTask.index.c_str());
	if(displayIndex<1||displayIndex>pendingList.size())
		flag = false;
	
		
	id = pendingList[displayIndex-1].getId();
	date=id.substr(0,8);
	dateObj=historyList[0].dateStringToObject(date);
	dateIndex=historyList[0].dateDifference(dateObj);
	indexToEdit=historyList[dateIndex].getIndex(id);
	eventObj = pendingList[displayIndex-1];
		

	if (indexToEdit==-1)
		flag = false;
	if (currentTask.date=="")
		flag =false;	
		
	if (flag)
	{
		Time timeToEdit;
		Time tempTime;

		if (currentTask.time!="")
		{
			if (tempTime.getStartEndTime(currentTask.time, time))
			{
				if (eventObj.getEventType() =="d" && time[1].getHour() !=0)
					flag = false;
					
				else
				{
					convertedTime << time[0].formatTime();
					if(time[1].getHour()!=0)
						convertedTime << '-' << time[1].formatTime();
					currentTask.time = convertedTime.str();
					historyList[dateIndex].editEvent(indexToEdit,currentTask,time[0],time[1]);
				}
			}
			else
				flag = false;
		}
	}

	if (flag)
	{
		//date and time together...time right date wrong??
		Event eventObj2 = historyList[dateIndex].getEvent(indexToEdit);
		deleteEvent(historyList, pendingList, currentTask);
				
		newTask.name = eventObj2.getName();
		newTask.location = eventObj2.getLocation();
		newTask.time = eventObj2.getTime ();
		newTask.date = currentTask.date;
		newTask.status=eventObj2.getEventStatus();
		newTask.deadline = eventObj2.getEventType();
				

		if (!listObj.addEvent (currentList, newTask))
		{
			newTask.time = eventObj.getTime();
			newTask.id=eventObj.getId();
			newTask.date=eventObj.getId().substr(0,8);
			addEvent(historyList, newTask);
			flag = false;
		}
		//else
		//{
		//	dateObj=currentList[0].dateStringToObject(currentTask.date);
		//	//statusMessage = currentList[dateIndex].getEventDetails(indexToEdit);
		//	dateIndex=currentList[0].dateDifference(dateObj);
		//	indexToEdit=currentList[dateIndex].getIndex(currentList[dateIndex].getLargestId());//ndex(current);
		//}
	}
	return flag;
}

bool ProcessHistory::markAsDone (vector<Date>& historyList, vector<Event>& pendingList, struct Task currentTask)
{
	bool flag=true;
	int displayIndex, i;
	Event eventObj;
	
	displayIndex=atoi(currentTask.index.c_str());
	if(displayIndex<1||displayIndex>pendingList.size())
	{
		flag=false;
	}

	else
	{
		string id = pendingList[displayIndex-1].getId();
		string date=id.substr(0,8);
		Date dateObj=historyList[0].dateStringToObject(date);
		int dateIndex=historyList[0].dateDifference(dateObj);
		int indexToMark=historyList[dateIndex].getIndex(id);
				
		if (indexToMark!=-1)
		{
			eventObj=historyList[dateIndex].getEvent(indexToMark);
			historyList[dateIndex].markEventAsDone(indexToMark);
			//statusMessage = currentList[dateIndex].getEventDetails(indexToDelete);
			//commandOrder.push("done");
			//eventOrder.push(eventObj);
		}
		else
			flag=false;
	}
	return flag;
}

void ProcessHistory::showHistory (vector<Date>& historyList, vector<Event>& pendingList)
{
	pendingList.clear();
	for (int i =0; i <historyList.size();i++)
	{
		for (int j = 0; j<historyList[i].getEventListLength(); j++)
		{
			pendingList.push_back(historyList.at(i).getEvent(j));
		}
	}
}

bool ProcessHistory::hasString(string userInput, string stringToBeSearched)
{
	int found;
	found=stringToBeSearched.find(userInput);
	if(found!=string::npos)
		return true;
	return false;
}

bool ProcessHistory::showEvents (vector<Date>& historyList, vector<Event>& pendingList ,struct Task currentTask)
{
	bool flag = true;
	Date date;
	if (currentTask.name!="")
	{
		pendingList.clear();
		for (int i =0; i <historyList.size();i++)
		{
			for (int j = 0; j<historyList[i].getEventListLength(); j++)
			{
				if (hasString(currentTask.name, historyList[i].getEvent(j).getName()))
					pendingList.push_back(historyList.at(i).getEvent(j));
			}
		}
	}
	

	else if (currentTask.date!="")
	{
		int index;
		Date currentDate;
		currentDate=historyList[0];
		Date dateToDisplay=currentDate.dateStringToObject(currentTask.date);
		
		if(currentDate.isValidDate(dateToDisplay))
		{
			 index = historyList[0].dateDifference(dateToDisplay);
			 if (index>=historyList.size())
				 flag=false;
			 else
			 {
				 pendingList.clear();
				 pendingList = historyList[index].getEventList();
			 }
		}
		else
			flag = false;	
	}

	else if (currentTask.location!="")
	{
		pendingList.clear();
		for (int i =0; i <historyList.size();i++)
		{
			for (int j = 0; j<historyList[i].getEventList().size(); j++)
			{
				if (hasString(currentTask.location, historyList[i].getEvent(j).getLocation()))
					pendingList.push_back(historyList[i].getEvent(j));
			}
		}
	}
	if (!flag)
		pendingList.clear();

	return flag;

}



ProcessHistory::~ProcessHistory(void)
{
}










